package com.qdlc.p2p.biz.service.impl;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.qdlc.p2p.biz.service.AdditionalDrawLotteryRuleService;
import com.qdlc.p2p.dal.common.model.jpa.PageDataList;
import com.qdlc.p2p.dal.dao.AdditionalDrawLotteryRuleDao;
import com.qdlc.p2p.dal.dto.AdditionalDrawLotteryRule;
import com.qdlc.p2p.dal.dto.Operator;
import com.qdlc.p2p.dal.model.AdditionalDrawLotteryRuleModel;

/**
 * 抽奖规则service
 * Created by liaoxiangkai on 2015/11/12.
 */
@Service("additionalDrawLotteryRuleService")
public class AdditionalDrawLotteryRuleServiceImpl implements AdditionalDrawLotteryRuleService {

    @Resource
    private AdditionalDrawLotteryRuleDao additionalDrawLotteryRuleDao;

    /**
     * 通过id查询抽奖规则
     *
     * @param lotteryRuleId
     * @return
     */
    @Override
    public AdditionalDrawLotteryRuleModel find(long lotteryRuleId) {
        AdditionalDrawLotteryRule entity = additionalDrawLotteryRuleDao.find(lotteryRuleId);
        if (null == entity) return null;
        AdditionalDrawLotteryRuleModel additionalDrawLotteryRuleModel = AdditionalDrawLotteryRuleModel.instance(entity);
        //将修改人名字设置给model
        Operator modifier = entity.getModifier();
        if (null != modifier)
        additionalDrawLotteryRuleModel.setModifierName(modifier.getUserName());

        //把创建人名字设置给model
        Operator creator = entity.getCreator();
        if (null != creator)
        additionalDrawLotteryRuleModel.setCreatorName(entity.getCreator().getUserName());
        return additionalDrawLotteryRuleModel;
    }

    /**
     * 查询抽奖规则列表
     *
     * @param model
     * @return
     */
    @Override
    public PageDataList<AdditionalDrawLotteryRuleModel> list(AdditionalDrawLotteryRuleModel model) {
        return additionalDrawLotteryRuleDao.list(model);
    }

    /**
     * 保存抽奖规则
     *
     * @param model
     */
    @Override
    public AdditionalDrawLotteryRule save(AdditionalDrawLotteryRuleModel model) {
        AdditionalDrawLotteryRule additionalDrawLotteryRule = inverseModel(model);
        additionalDrawLotteryRule.setCreator(model.getCreator());
        additionalDrawLotteryRule.setCreateTime(model.getCreateTime());
        return additionalDrawLotteryRuleDao.save(additionalDrawLotteryRule);
    }

    /**
     * 更新抽奖规则
     *
     * @param model
     */
    @Override
    public AdditionalDrawLotteryRule update(AdditionalDrawLotteryRuleModel model) {
        AdditionalDrawLotteryRule additionalDrawLotteryRule = inverseModel(model);
        additionalDrawLotteryRule.setId(model.getId());
        additionalDrawLotteryRule.setModifier(model.getModifier());
        additionalDrawLotteryRule.setModifyTime(model.getModifyTime());
        AdditionalDrawLotteryRule findRule = additionalDrawLotteryRuleDao.find(model.getId());
        if (null == findRule) return null;
        additionalDrawLotteryRule.setCreator(findRule.getCreator());
        additionalDrawLotteryRule.setCreateTime(findRule.getCreateTime());
        return additionalDrawLotteryRuleDao.update(additionalDrawLotteryRule);
    }

    /**
     * 切换规则状态0：开启，1：关闭
     *
     * @param model
     */
    @Override
    public int toggleStatus(AdditionalDrawLotteryRuleModel model) {
        return additionalDrawLotteryRuleDao.toggleStatus(model);
    }

    /**
     * 将model转成他的基类
     *
     * @param model
     * @return
     */
    public AdditionalDrawLotteryRule inverseModel(AdditionalDrawLotteryRuleModel model) {
        AdditionalDrawLotteryRule additionalDrawLotteryRule = new AdditionalDrawLotteryRule();
        additionalDrawLotteryRule.setName(model.getName());
        additionalDrawLotteryRule.setStatus(model.getStatus());
        additionalDrawLotteryRule.setCount(model.getCount());
        additionalDrawLotteryRule.setValidDay(model.getValidDay());
        additionalDrawLotteryRule.setContinueCount(model.getContinueCount());
        additionalDrawLotteryRule.setDescription(model.getDescription());
        return additionalDrawLotteryRule;
    }
}
